package com.situ.analytics.api;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.mall.analytics.UserStatsVO;
import com.mall.common.util.JsonResult;
import com.situ.analytics.service.client.UserClient;
import com.situ.analytics.service.client.ProductClient;
import com.situ.analytics.service.client.SalesClient;
import com.situ.mall.analytics.spi.model.back.ProductBack;
import com.situ.mall.analytics.spi.model.back.Sales;
import com.situ.mall.analytics.spi.model.utils.TimeSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.*;

@RefreshScope
@RestController
@RequestMapping(value = "/api/analytics", produces = "application/json;charset=utf-8")
public class AnalyticsApi {

    private SalesClient salesClient;
    private ProductClient productClient;
    private UserClient UserClient;

    @Autowired
    public void setProductClient(ProductClient productClient) {
        this.productClient = productClient;
    }

    @Autowired
    public void setUserClient(UserClient UserClient) {
        this.UserClient = UserClient;
    }

    @Autowired
    public void setSalesClient(SalesClient salesClient) {
        this.salesClient = salesClient;
    }

    @PostMapping("/sale-key-metrics")
    public ResponseEntity<JsonResult<?>> getKeyMetrics(@RequestBody TimeSearch timeSearch){
        System.out.println(timeSearch);
        System.out.println("启动了sale-key-metrics");
        LocalDate startDate = timeSearch.getStartDate();
        LocalDate endDate = timeSearch.getEndDate();
        Sales sales = salesClient.getKeyMetrics(startDate,endDate);
        System.out.println("sale-key-metrics:"+sales);
        return ResponseEntity.ok(JsonResult.success(sales));
    }

    @PostMapping("/sale-pay-type")
    public ResponseEntity<JsonResult<?>> getPayType(@RequestBody TimeSearch timeSearch){
        System.out.println("启动了sale-pay-type");
        LocalDate startDate = timeSearch.getStartDate();
        LocalDate endDate = timeSearch.getEndDate();

        try {
            Object payTypeResult = salesClient.getPayType(startDate, endDate);
            System.out.println("sale-pay-type:"+payTypeResult);
            // 处理可能的不同返回类型
            List<Map<String, Object>> result;
            if (payTypeResult instanceof List) {
                result = (List<Map<String, Object>>) payTypeResult;
            } else if (payTypeResult instanceof Map) {
                // 如果返回的是单个对象，包装成列表
                result = Arrays.asList((Map<String, Object>) payTypeResult);
            } else {
                // 如果返回其他类型，创建空列表
                result = new ArrayList<>();
            }


            return ResponseEntity.ok(JsonResult.success(result));
        } catch (Exception e) {
            return ResponseEntity.ok(JsonResult.success(new ArrayList<>()));
        }
    }
    @PostMapping("/product-key-metrics")
    public ResponseEntity<JsonResult<?>> getProductKeyMetrics(@RequestBody TimeSearch timeSearch){
        System.out.println("启动了product-key-metrics");
        LocalDate startDate = timeSearch.getStartDate();
        LocalDate endDate = timeSearch.getEndDate();
        ProductBack productBack = productClient.getKeyMetrics(startDate, endDate);
        System.out.println("product-key-metrics:"+productBack);
        return ResponseEntity.ok(JsonResult.success(productBack));
    }
    @PostMapping("/product-stock")
    public ResponseEntity<JsonResult<?>> getProductStock(@RequestBody TimeSearch timeSearch){
        System.out.println("启动了product-stock");
        LocalDate startDate = timeSearch.getStartDate();
        LocalDate endDate = timeSearch.getEndDate();
        List<Map<String,Object>> list= productClient.getStockBack(startDate, endDate);
        System.out.println("product-stock:"+list);
        return ResponseEntity.ok(JsonResult.success(list));
    }

    @PostMapping("/product-category")
    public ResponseEntity<JsonResult<?>> getProductCategory(@RequestBody TimeSearch timeSearch){
        System.out.println("启动了product-category");
        LocalDate startDate = timeSearch.getStartDate();
        LocalDate endDate = timeSearch.getEndDate();
        List<Map<String,Object>> list= productClient.getProductCount(startDate, endDate);
        System.out.println("product-category:"+list);
        return ResponseEntity.ok(JsonResult.success(list));
    }

    @GetMapping("/user-status")
    public ResponseEntity<JsonResult<?>> getUserStatus(){
        System.out.println("启动了user-status");
        JsonResult<UserStatsVO> userStatsVO = UserClient.getUserStatsVO();
        System.out.println("user-status:"+userStatsVO);
        return ResponseEntity.ok(userStatsVO);
    }
    @GetMapping("/test")
    public ResponseEntity<JsonResult<?>> test(){
        System.out.println("启动了test");
        System.out.println("测试成功");
        return ResponseEntity.ok(JsonResult.success("测试成功"));
    }

    //熔断限流
    public ResponseEntity<JsonResult<?>> blockTest(BlockException blockException){
        System.out.println("启动了block");
        return ResponseEntity.ok(JsonResult.fail("测试限流"));
    }
    //普通业务异常
    public ResponseEntity<JsonResult<?>> testFallback(Throwable throwable){
        System.out.println("启动了testFallback");
        return ResponseEntity.ok(JsonResult.fail("测试失败"));
    }
}
